package com.xmkj.bearbiz.pk;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.BearBean.DaoGouPKBean;
import com.common.BearBean.HtmlBean;
import com.common.BearMethods.GetDaoPKMethods;
import com.common.BearMethods.GetHtmlMethods;
import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.base.Constants;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.widget.StarBarView;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearbiz.BaseUrlURLActivity;
import com.xmkj.bearbiz.R;
import com.xmkj.bearbiz.mine.activity.InfoCompileActivity;
import com.xmkj.bearbiz.target.adapter.PkDaoGouAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.functions.Action1;

/**
 * Create by leo
 * 创建日期：2018/6/13
 * 描述：
 */

public class PkFragment extends BaseMvpFragment {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.starbar)
    StarBarView starbar;
    @BindView(R.id.tv_info_weifu)
    TextView tvInfoWeifu;
    @BindView(R.id.tv_jili_rule)
    TextView tvJiliRule;
    @BindView(R.id.tv_jifen_rule)
    TextView tvJifenRule;
    @BindView(R.id.tv_shop_communication)
    TextView tvShopCommunication;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    @BindView(R.id.iv_img)
    ImageView ivImg;

    Unbinder unbinder;
    private DaoGouPKBean bean;
    private PkDaoGouAdapter topFourAdapter;
    private HtmlBean htmlBean;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_pk;
    }

    @Override
    protected void onViewCreated(View view) {

    }

    @Override
    protected void doLogicFunc() {
        attachClickListener(tvInfoWeifu);
        attachClickListener(tvShopCommunication);
        attachClickListener(tvJifenRule);
        attachClickListener(tvJiliRule);
        getMemberData();
        getHtml();
        starbar.setEnabled(false);
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.UPDATE_INFO) && (Boolean) rxKeyEvent.getValue()) {
                    getMemberData();

                }
            }
        }));
    }

    private void getHtml() {
        CommonSubscriber<HtmlBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                htmlBean = (HtmlBean) o;

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

//        jpushid = JPushInterface.getRegistrationID(this);
        GetHtmlMethods.getInstance().getH5(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID));
        rxManager.add(subscriber);
    }

    private void getMemberData() {
        CommonSubscriber<DaoGouPKBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                bean = (DaoGouPKBean) o;
                if(EmptyUtils.isNotEmpty(bean.mystar.nickname)){
                    tvName.setText(bean.mystar.nickname);
                    SPUtils.setShareString(Constants.USERNAME, bean.mystar.nickname);
//                    ImageLoaderUtils.displayCircle(ivImg,bean.mystar.);

                }else {
                    tvName.setText("");
                }
                    starbar.setStarRating(Integer.valueOf(bean.mystar.star_level));
                    setReclerview();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

//        jpushid = JPushInterface.getRegistrationID(this);
        GetDaoPKMethods.getInstance().daogouPK(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID));
        rxManager.add(subscriber);
    }
    private void setReclerview() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);

//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(true);
        recyclerview.setLoadMoreEnabled(true);
        List<DaoGouPKBean.DaogouListEntity> beanList = bean.daogou_list;
        DaoGouPKBean.DaogouListEntity myItem = new DaoGouPKBean.DaogouListEntity();
        myItem.deptname = bean.mystar.deptname;
        myItem.nickname = bean.mystar.deptname;
        myItem.score = bean.mystar.score;
        myItem.star_level = bean.mystar.star_level;
        beanList.add(myItem);
        topFourAdapter = new PkDaoGouAdapter(context, beanList);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(topFourAdapter);
        recyclerview.setRefreshEnabled(false);
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == tvInfoWeifu.getId()) {
            gotoActivity(InfoCompileActivity.class);
        }else if(view.getId()==tvShopCommunication.getId()){
            gotoActivity(ShopCommunicationActivity.class);
        }else if(view.getId()==tvJifenRule.getId()){
            Intent intent1 = new Intent(context, BaseUrlURLActivity.class);
            intent1.putExtra(BaseUrlURLActivity.MAIN_URL, htmlBean.score);
            intent1.putExtra(BaseUrlURLActivity.TITILE, "积分规则");
            context.startActivity(intent1);
        }else if(view.getId()==tvJiliRule.getId()){
            Intent intent1 = new Intent(context, BaseUrlURLActivity.class);
            intent1.putExtra(BaseUrlURLActivity.MAIN_URL, htmlBean.daogou);
            intent1.putExtra(BaseUrlURLActivity.TITILE, "激励规则");
            context.startActivity(intent1);
        }
    }

    @Override
    public void bindKnife() {
        unbinder = ButterKnife.bind(this, view);
    }
}
